]> git.r.bdr.sh - rbdr/Flat-Bezel.qsplugin/blobdiff - Flat Bezel/FlatBezelObjectCell.swift
Port more files to swift
[rbdr/Flat-Bezel.qsplugin] / Flat Bezel / FlatBezelObjectCell.swift
diff --git a/Flat Bezel/FlatBezelObjectCell.swift b/Flat Bezel/FlatBezelObjectCell.swift
new file mode 100644 (file)
index 0000000..da38a1b
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+import Foundation
+
+class FlatBezelObjectCell: QSObjectCell {
+  override func preferredImagePosition() -> NSControl.ImagePosition {
+      return .imageAbove
+  }
+  
+  override func draw(withFrame cellFrame: NSRect, in controlView: NSView?) {
+    if let controlView {
+      
+      let isFirstResponder = controlView.window?.firstResponder == controlView && !(controlView is NSTableView)
+      let dropTarget = isHighlighted && ((highlightsBy.rawValue & NSCell.StyleMask.changeBackgroundCellMask.rawValue) != 0) && !isBezeled
+      
+      var fillColor: NSColor?
+      let strokeColor = NSColor.clear
+      
+      if isFirstResponder {
+        fillColor = self.highlightColor()
+      } else {
+        fillColor = self.backgroundColor
+      }
+      
+      if dropTarget {
+        fillColor = NSColor(red: 0.77, green: 0.91, blue: 0.96, alpha: 1)
+      }
+      
+      fillColor?.setFill()
+      strokeColor.setStroke()
+      
+      let roundRect = NSBezierPath()
+      roundRect.append(withRoundedRectangle: cellFrame, withRadius: cellRadiusFactor())
+      roundRect.fill()
+      
+      drawInterior(withFrame: drawingRect(forBounds: cellFrame), in: controlView)
+    }
+  }
+  
+  override func titleRect(forBounds _rect: NSRect) -> NSRect {
+      var rect = _rect
+      rect = NSOffsetRect(rect, 0, -4)
+      return super.titleRect(forBounds: rect)
+  }
+  
+  override func drawText(for drawObject: QSObject!, withFrame cellFrame: NSRect, in controlView: NSView!) {
+    if imagePosition == .imageOnly {
+      return
+    }
+    
+    var abbrString: String? = nil
+    
+    if controlView.responds(to: #selector(QSSearchObjectView.matchedString)) {
+      abbrString = (controlView as! QSSearchObjectView).matchedString()
+    }
+    
+    var nameString: String? = nil
+    var hitMask: NSIndexSet? = nil
+    
+    let ranker = drawObject.ranker()
+    if ranker != nil && abbrString != nil {
+      nameString = ranker?.matchedString(forAbbreviation: abbrString, hitmask: &hitMask, inContext: nil)
+    }
+    
+    if (nameString == nil) {
+      nameString = drawObject.displayName()
+    }
+    
+    let rankedStringIsName = nameString == drawObject.displayName() || nameString == nil
+    
+    if nameString == nil {
+      nameString = drawObject.identifier() ?? "Unknown"
+    }
+    
+    let useAlternateColor = controlView is NSTableView && (controlView as! NSTableView).isRowSelected((controlView as! NSTableView).row(at: cellFrame.origin))
+    let mainColor = textColor() ?? (useAlternateColor ? .alternateSelectedControlTextColor : .controlTextColor)
+    let fadedColor = mainColor.withAlphaComponent(0.50)
+    let textDrawRect = titleRect(forBounds: cellFrame)
+    
+    
+    let titleString = NSMutableAttributedString(string: nameString!)
+    let nameAttributes = (value(forKey: "nameAttributes") as? [NSAttributedString.Key : Any])
+    let detailsAttributes = (value(forKey: "detailsAttributes") as?  [NSAttributedString.Key : Any])
+    titleString.setAttributes(rankedStringIsName ? nameAttributes : detailsAttributes, range: NSMakeRange(0, titleString.length))
+    
+    if abbrString != nil && abbrString!.hasPrefix("QSActionMnemonic") {
+      titleString.addAttribute(.foregroundColor, value: rankedStringIsName ? fadedColor : fadedColor.withAlphaComponent(0.8), range: NSMakeRange(0, titleString.length))
+      
+      var hits: Int = 0
+      let count = hitMask?.getIndexes(&hits, maxCount: titleString.length, inIndexRange: nil)
+      for i in 0..<(count ?? 0) {
+        for j in 1.. {
+        }
+      }
+    }
+}
+
+    NSUInteger i = 0;
+    NSUInteger j = 0;
+    NSUInteger hits[[titleString length]];
+    NSUInteger count = [hitMask getIndexes:(NSUInteger *)&hits maxCount:[titleString length] inIndexRange:nil];
+    NSDictionary *attributes = @{
+      NSForegroundColorAttributeName:                                 rankedStringIsName ? mainColor : fadedColor
+    };
+    for(i = 0; i<count; i += j) {
+      for (j = 1; i+j<count && hits[i+j-1] +1 == hits[i+j]; j++);
+      [titleString addAttributes:attributes range:NSMakeRange(hits[i], j)];
+    }
+  } else {
+    [titleString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, [titleString length])];
+  }
+
+  if (showDetails) {
+    NSString *detailsString = [drawObject details];
+    
+    NSRange returnRange = [detailsString rangeOfString:@"\n"];
+    if (returnRange.location != NSNotFound) {
+      detailsString = [detailsString substringToIndex:returnRange.location];
+    }
+
+    detailsAttributes = [detailsAttributes mutableCopy];
+    [detailsAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
+    
+    if (detailsString && detailsString.length && ![detailsString isEqualToString:nameString]) {
+      [titleString appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",detailsString] attributes:detailsAttributes] autorelease]];
+    }
+  }
+
+  NSRect centerRect = rectFromSize([titleString size]);
+  centerRect.size.width = NSWidth(textDrawRect);
+  centerRect.size.height = MIN(NSHeight(textDrawRect), centerRect.size.height);
+  [titleString drawInRect:centerRectInRect(centerRect, textDrawRect)];
+}
+
+- (void)drawSearchPlaceholderWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
+  NSString *defaultText = NSLocalizedStringWithDefaultValue(@"Type to search", nil, [NSBundle mainBundle], @"Type to search", @"Hint that appears in the first pane of the QS interface when it's empty.");
+  NSSize textSize = [defaultText sizeWithAttributes:nameAttributes];
+  NSRect textRect = centerRectInRect(rectFromSize(textSize), cellFrame);
+  BOOL isFirstResponder = [[controlView window] firstResponder] == controlView && ![controlView isKindOfClass:[NSTableView class]];
+  
+  if (isFirstResponder && [controlView isKindOfClass:[QSSearchObjectView class]]) {
+    NSImage *find = [NSImage imageWithSystemSymbolName:@"magnifyingglass.circle.fill" accessibilityDescription:nil];
+    
+    
+    [find setSize:QSSize16];
+    NSRect findImageRect = expelRectFromRectOnEdge(centerRectInRect(rectFromSize([find size]), cellFrame), textRect, NSRectEdgeMinX, -2);
+    
+  
+    NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
+    [graphicsContext saveGraphicsState];
+    CGContextRef context = [graphicsContext CGContext];
+    CGContextBeginTransparencyLayerWithRect(context, findImageRect, nil);
+    CGContextSetBlendMode(context, kCGBlendModeNormal);
+    [find drawInRect:findImageRect fromRect:rectFromSize([find size]) operation:NSCompositingOperationSourceOver fraction:1];
+    CGContextSetBlendMode(context, kCGBlendModeSourceIn);
+    CGContextSetFillColorWithColor(context, [[NSColor textColor] CGColor]);
+    CGContextFillRect(context, findImageRect);
+    CGContextEndTransparencyLayer(context);
+
+    [defaultText drawInRect:textRect withAttributes:nameAttributes];
+  }
+}
+
+@end
+*/